package lib.self.util.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION_CODES;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.RelativeLayout;
import android.widget.Scroller;

import java.lang.reflect.Field;

import lib.self.AppEx;
import lib.self.utils.DeviceUtil;

/**
 * description: View的工具类
 * author: Steven Xiao
 * date: 2017/4/26
 */
public class ViewUtil {
    /**
     * 控件显示
     */
    public static void showView(View view) {
        if (view.getVisibility() != View.VISIBLE) {
            view.setVisibility(View.VISIBLE);
        }
    }

    public static void showView(View... views) {
        for (View view : views) {
            showView(view);
        }
    }

    /**
     * 控件消失
     */
    public static void goneView(View view) {
        if (view.getVisibility() != View.GONE) {
            view.setVisibility(View.GONE);
        }
    }

    public static void goneView(View... views) {
        for (View view : views) {
            goneView(view);
        }
    }

    /**
     * 隐藏控件
     */
    public static void hideView(View view) {
        if (view.getVisibility() != View.INVISIBLE) {
            view.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 设置背景
     */
    @TargetApi(VERSION_CODES.JELLY_BEAN)
    public static void setBackground(View view, Drawable background) {
        if (DeviceUtil.getSDKVersion() >= VERSION_CODES.JELLY_BEAN) {
            view.setBackground(background);
        } else {
            view.setBackgroundDrawable(background);
        }
    }

    public static View inflateSpaceViewByPx(int px) {
        Context context = AppEx.getCt();
        RelativeLayout layout = new RelativeLayout(context);
        View v = new View(context);
        layout.addView(v, LayoutUtil.getRelativeParams(LayoutUtil.MATCH_PARENT, px));
        return layout;
    }

    /**
     * 可设置间隔的scroller
     */
    public static class FixedSpeedScroller extends Scroller {
        private int mDuration = 250;

        public FixedSpeedScroller(Context context) {
            super(context);
        }

        public FixedSpeedScroller(Context context, Interpolator interpolator) {
            super(context, interpolator);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            // Ignore received duration, use fixed one instead
            super.startScroll(startX, startY, dx, dy, mDuration);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy) {
            // Ignore received duration, use fixed one instead
            super.startScroll(startX, startY, dx, dy, mDuration);
        }

        public void setDuration(int time) {
            mDuration = time;
        }
    }

    public static FixedSpeedScroller setViewPagerScrollDuration(ViewPager viewPager, int duration) {
        FixedSpeedScroller scroller = new FixedSpeedScroller(viewPager.getContext(), new DecelerateInterpolator());
        try {
            Field mField = ViewPager.class.getDeclaredField("mScroller");
            mField.setAccessible(true);
            scroller.setDuration(duration);
            mField.set(viewPager, scroller);
            return scroller;
        } catch (Exception e) {
            return scroller;
        }
    }
}
